<template>
  <div class="hmcomment">
    <div class="addcomment" v-show="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="handlerFocus" />
      <span class="comment" @click="$router.push({path:'/comment/'+artive.id})">
        <i class="iconfont iconpinglun-"></i>
        <em>{{artive.comment_length}}</em>
      </span>
      <i class="iconfont iconshoucang" :class="{active:artive.has_star}" @click="artiveStar"></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus" @blur="isFocus=!isFocus">
      <textarea ref="commtext" rows="5" v-model.trim="comment.content"></textarea>
      <div>
        <span @click="postcomment">发 送</span>
        <span @click="cancelReplay">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { postStar, postComment } from "@/apis/post.js";
export default {
  props: {
    artive: {
      type: Object,
      required: true
    },
    commentObj: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      isFocus: false,
      comment: {
        content: ""
      }
    };
  },
  watch: {
    commentObj() {
      console.log("子组件中的", this.commentObj);
      if (this.commentObj) {
        this.isFocus = true;
      }
    }
  },
  methods: {
    async artiveStar() {
      let id = this.artive.id;
      let res = await postStar(id);
      this.$toast.success(res.data.message);
      this.artive.has_star = !this.artive.has_star;
    },
    handlerFocus() {
      this.isFocus = !this.isFocus;
    },
    // 发表评论
    async postcomment() {
      if (this.comment.content.length == 0) {
        this.$toast.fail("请输入评论内容");
        return;
      }
      let id = this.artive.id;
      // console.log(id, this.comment.content);
      if (this.commentObj) {
        this.comment.parent_id = this.commentObj.id;
      }

      let res = await postComment(id, this.comment);

      // console.log(res);
      this.$toast.success(res.data.message);

      // 隐藏输入框
      this.isFocus = false;
      // 清空评论内容
      this.comment.content = "";

      // 页面内容刷新，子组件要告诉父组件进行列表数据的刷新
      this.$emit("refresh");
    },

    cancelReplay() {
      // 关闭评论框
      this.isFocus = !this.isFocus;

      this.$emit("cancel");
    }
  }
};
</script>

<style lang="less" scoped>
.hmcomment {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
.inputcomment {
  // position: absolute;
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  background-color: #fff;

  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
.active {
  color: red;
}
</style>